package com.rzj.book.controller;

import com.alibaba.fastjson.JSON;
import com.rzj.book.en.*;
import com.rzj.book.service.ICategoryService;
import com.rzj.book.service.IEmpService;
import com.rzj.book.service.IRoleService;
import com.rzj.book.utils.PageResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName AdminController
 * @Description TODO
 * @Author Administrator
 * @Date 2020/5/17 13:30
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin")
public class AdminController extends BaseController {

    @Autowired
    private IEmpService empService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private ICategoryService categoryService;

    @RequestMapping("/menu_getMenuTree")
    public void getMenuTree() {
        //通过获取主菜单，自关联就会带其下所有的菜单
        //Menu menu = menuService.get("0");
        Menu menu = empService.readMenusByEmpuuid(getLoginUser().getEmpId());
        write(JSON.toJSONString(menu));
    }

    @RequestMapping("/getRole_readRoleMenus")
    public void getRole_readRoleMenus(Integer id) {
        List<Tree> menus = roleService.readRoleMenus(id);
        write(JSON.toJSONString(menus));
    }

    @RequestMapping("/emp_readEmpRoles")
    public void emp_readEmpRoles(Integer id) {
        List<Tree> list = empService.readEmpRoles(id);
        write(JSON.toJSONString(list));
    }

    @RequestMapping("/emp_list")
    public void emp_list() {
        List<Emp> emps = empService.getEmpList();
        write(JSON.toJSONString(emps));
    }

    @RequestMapping("/getRole_list")
    public void getRole_list() {
        List<Role> roles = roleService.getRoleList();
        write(JSON.toJSONString(roles));
    }

    @RequestMapping("/getEmpinfo")
   public void getEmpinfo(){
       Emp emp = (Emp) SecurityUtils.getSubject().getPrincipal();
       write(JSON.toJSONString(emp));
    }

    @RequestMapping("/login")
    public void login(Emp emp)
    {
        try{
            UsernamePasswordToken upt=new UsernamePasswordToken(emp.getUsername(), emp.getPwd());
            //2.获取主题：封装用户的一些操作
            Subject subject=SecurityUtils.getSubject();
            //3.执行登录
            subject.login(upt);

           ajaxReturn(true, "");
        }catch(UnknownAccountException e){
            e.printStackTrace();
            ajaxReturn(false, "用户名或密码错误");
        }catch(Exception e){
            e.printStackTrace();
            ajaxReturn(false, "未知错误");
        }
    }

    @RequestMapping("/login_showName")
    public void showName(){
        Emp emp = (Emp) getSession().getAttribute("loginUser");

        if(null != emp){
            ajaxReturn(true, JSON.toJSONString(emp));
        }else{
            ajaxReturn(false, "");
        }
    }

    @RequestMapping("/loginOut")
    public void loginOut(){
       getSession().invalidate();
    }


    @RequestMapping("/category_list")
    public void getCategory_list(Integer page,Integer rows,Category category){
      PageResult<Category> categoryList = categoryService.getListByCategort(page,rows,category);
      write(JSON.toJSONString(categoryList));
    }

}
